<?

//*********************************************
// NOMBRE DE LAS TABLAS
//*********************************************
$select_tables_name      = "SELECT * FROM ".$ecrmDB."..SYSOBJECTS WHERE name LIKE 'cat%' AND name NOT LIKE 'cat_GuionTipoDato' AND name NOT LIKE 'cat_GuionTipoPregunta' AND name NOT LIKE 'cat_GuionTipoSeleccion'";
//fun_Echo("SELECCIONA LOS CATALOGOS","red",$select_tables_name);
$rselect_tables_name     = mssql_query($select_tables_name);

if($catplgxEnable == 0) {
    $enabled_cat = "readonly disabled";
}else{
    $enabled_cat = "";
}


if ($rowrselect_tables_name = mssql_fetch_array($rselect_tables_name)){
    $tableNameList  = "<select name=\"cat_table\" onChange=\"Enviar2();\" ".$enabled_cat.">\n";
    $tableNameList .=  "<option value=\"0\">Selecciona el cat&aacute;logo ----></option>\n";
    
    do {

        if($_POST['cat_table'] == $rowrselect_tables_name['name']) {
            $selectedtable = "selected";
        }else{
            $selectedtable = "";
        }

        $tableNameList .=  "<option value=\"".$rowrselect_tables_name['name']."\" ".$selectedtable.">".$rowrselect_tables_name['name']."\n";
        
    }while ($rowrselect_tables_name = mssql_fetch_array($rselect_tables_name));

    $tableNameList .= "</select>\n";

}else{
    $tableNameList = "<select name=\"\" disabled readonly>\n";
    $tableNameList .=  "<option value=\"0\">No hay cat&aacute;logos</option>\n";
    
}

$tableNameList .= "</select>\n";

if($proceso == 2) {

    $nombres_campos = "SELECT * FROM ".$ecrmDB."..".$_POST['cat_table'];
    //fun_Echo("SELECCIONA LOS CAMPOS","red",$select_tables_name);
    $rnombres_campos     = mssql_query($nombres_campos);


    if ($fld = mssql_fetch_field($rnombres_campos)){

        $tableCatalogIDx  = "<select name=\"cat_id\" ".$enabled_cat.">\n";
        $tableCatalogIDx .=  "<option value=\"0\">Selecciona el campo ----></option>\n";
        $tableCatalogTextox  = "<select name=\"cat_txt\" ".$enabled_cat.">\n";
        $tableCatalogTextox .=  "<option value=\"0\">Selecciona el campo ----></option>\n";

        do {

            $tableCatalogIDx .=  "<option value=\"".$fld->name."\">".$fld->name."\n";
            $tableCatalogTextox .=  "<option value=\"".$fld->name."\">".$fld->name."\n";

        }while($fld = mssql_fetch_field($rnombres_campos));

        $tableCatalogIDx .= "</select>\n";
        $tableCatalogTextox .= "</select>\n";

    }else{
        $tableCatalogIDx = "<select name=\"cat_id\" disabled readonly>\n";
        $tableCatalogIDx .=  "<option value=\"0\">No hay campos en la tabla</option>\n";
        $tableCatalogTextox = "<select name=\"cat_txt\" disabled readonly>\n";
        $tableCatalogTextox .=  "<option value=\"0\">No hay campos en la tabla</option>\n";

    }

}

if($catplgxStartResult == 1) {
$selectThis = "";

if((int) $row_01['catalogoNueves']==0) {
  $no_nueves = " where ".$row_01['catalogoId']." != 9999 ";
}else{
  $no_nueves = "";
}

$select_select_cat   = "SELECT * FROM ".$ecrmDB."..".$row_01['catalogoTable'].$no_nueves;
$rselect_select_cat  = mssql_query($select_select_cat);
if ($rowrrselect_select_cat = mssql_fetch_array($rselect_select_cat)){

$select_cat_finished = "";

if($row_01['catalogoInput'] == 1){    // COMBO

    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= "if((forma.pregunta_cat".$row_01['id_GuionPregunta'].".selectedIndex == 0) && (error == 0)) {
        fixElement(forma.pregunta_cat".$row_01['id_GuionPregunta'].", \"".$row_01['msgObligatoria']."\");
        error = 1;
        }";
    }
    // ********** END SI ES OBLIGATORIA *************

    $RespuestaUsuario = 0;
    if($etUpdate == 1) {
        // *****************************************************************************
        // SELECCIONAMOS EL VALOR ACTUAL DEL USUARIO
        $selectDatos = mssql_query("SELECT tableName, fieldName FROM app_DBConnections where id_DBConnection = ".$row_01['id_DBConnection']);
        if($DatosConnection = mssql_fetch_array($selectDatos)) {
            $campoAUpdate = $DatosConnection['fieldName'];
            if(strstr($campoAUpdate,"id_UsuarioWeb")) {
                $campoAUpdate = str_replace("id_UsuarioWeb","",$campoAUpdate);
                $campoAUpdate = trim(str_replace(",","",$campoAUpdate));
            }
            $SelectUserAnswers = mssql_query("SELECT ".$campoAUpdate." FROM ".$ecrmDB."..".$DatosConnection['tableName']." WHERE id_UsuarioWeb = ".$id_UsuarioWeb);
            $UserAnswer = mssql_fetch_array($SelectUserAnswers);
            $RespuestaUsuario = $UserAnswer[$campoAUpdate];
        }else{
            $RespuestaUsuario = 0;
        }
        // END SELECCIONAMOS EL VALOR ACTUAL DEL USUARIO
        // *****************************************************************************
    }

    $select_cat_finished .= "<select name=\"pregunta_cat".$row_01['id_GuionPregunta']."\" ".$enabled_cat.">\n";
    $select_cat_finished .=  "<option value=\"0\">Selecciona ----></option>\n";

    do {

        if($RespuestaUsuario == $rowrrselect_select_cat[$row_01['catalogoId']]) {
          $selectThis = "selected";
        }else{
          $selectThis = "";
        }

        $select_cat_finished .=  "<option value=\"".$rowrrselect_select_cat[$row_01['catalogoId']]."\" ".$selectThis.">".$rowrrselect_select_cat[$row_01['catalogoText']]."\n";

    }while ($rowrrselect_select_cat = mssql_fetch_array($rselect_select_cat));

    $select_cat_finished .= "</select>\n";
  
}else if($row_01['catalogoInput'] == 2){   // RADIO

    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= "if ((!(";
    }
    // ********** END SI ES OBLIGATORIA *************

    $RespuestaUsuario = 0;
    $novalida = 1;
    if($etUpdate == 1) {
        // *****************************************************************************
        // SELECCIONAMOS EL VALOR ACTUAL DEL USUARIO
        $selectDatos = mssql_query("SELECT tableName, fieldName FROM app_DBConnections where id_DBConnection = ".$row_01['id_DBConnection']);
        if($DatosConnection = mssql_fetch_array($selectDatos)) {
            $campoAUpdate = $DatosConnection['fieldName'];
            if(strstr($campoAUpdate,"id_UsuarioWeb")) {
                $campoAUpdate = str_replace("id_UsuarioWeb","",$campoAUpdate);
                $campoAUpdate = trim(str_replace(",","",$campoAUpdate));
            }
            $SelectUserAnswers = mssql_query("SELECT ".$campoAUpdate." FROM ".$ecrmDB."..".$DatosConnection['tableName']." WHERE id_UsuarioWeb = ".$id_UsuarioWeb);
            $UserAnswer = mssql_fetch_array($SelectUserAnswers);
            $RespuestaUsuario = $UserAnswer[$campoAUpdate];
            $novalida = 0;
        }else{
            $RespuestaUsuario = 0;
            $novalida = 1;
        }
        // END SELECCIONAMOS EL VALOR ACTUAL DEL USUARIO
        // *****************************************************************************
    }
    
    $_mnb = 0;
    $select_cat_finished .= "<div id=\"".$idChangeSize."\">";
    do {

        // ********** SI ES OBLIGATORIA *****************
        if($start_cat_valida == 1) {
            if($_mnb == 0) {
                $JavaIA_check .= "forma.pregunta_cat".$row_01['id_GuionPregunta']."[".$_mnb."].checked ";
            }else{
                $JavaIA_check .= "|| forma.pregunta_cat".$row_01['id_GuionPregunta']."[".$_mnb."].checked ";
            }
        }
        // ********** END SI ES OBLIGATORIA *************
        if($novalida == 0) {
            if($RespuestaUsuario == $rowrrselect_select_cat[$row_01['catalogoId']]) {
              $selectThis = "checked";
            }else{
              $selectThis = "";
            }
        }
    
    $select_cat_finished .=  "<input type=\"radio\" name=\"pregunta_cat".$row_01['id_GuionPregunta']."\" value=\"".$rowrrselect_select_cat[$row_01['catalogoId']]."\" ".$enabled_cat." ".$selectThis.">".$LetraActivacion.$rowrrselect_select_cat[$row_01['catalogoText']]."<br>\n";
        
    $_mnb++;
    }while ($rowrrselect_select_cat = mssql_fetch_array($rselect_select_cat));
    
    $select_cat_finished .= "</div>";
    
    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= ")) && (error == 0)) {
        alert(\"".$row_01['msgObligatoria']."\");
        error = 1;
        }\n";
    }
    // ********** END SI ES OBLIGATORIA *************
  
}else if($row_01['catalogoInput'] == 3){ // CHECKBOX

    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= "if(";
    }
    // ********** END SI ES OBLIGATORIA *************

    $RespuestaUsuario = 0;
    if($etUpdate == 1) {
        // *****************************************************************************
        // SELECCIONAMOS EL VALOR ACTUAL DEL USUARIO
        $selectDatos = mssql_query("SELECT tableName, fieldName FROM app_DBConnections where id_DBConnection = ".$row_01['id_DBConnection']);
        if($DatosConnection = mssql_fetch_array($selectDatos)) {
            $campoAUpdate = $DatosConnection['fieldName'];
            if(strstr($campoAUpdate,"id_UsuarioWeb")) {
                $campoAUpdate = str_replace("id_UsuarioWeb","",$campoAUpdate);
                $campoAUpdate = trim(str_replace(",","",$campoAUpdate));
            }
            $SelectUserAnswers = mssql_query("SELECT ".$campoAUpdate." FROM ".$ecrmDB."..".$DatosConnection['tableName']." WHERE id_UsuarioWeb = ".$id_UsuarioWeb);
            if($UserAnswer = mssql_fetch_array($SelectUserAnswers)) {
                $RespuestaUsuarioArreglo = array();
                $ArrayRespuesta = 0;
                do{
                   $RespuestaUsuarioArreglo[$ArrayRespuesta] = $UserAnswer[$campoAUpdate];
                   $ArrayRespuesta++;
                }while($UserAnswer = mssql_fetch_array($SelectUserAnswers));
            }else{
                $RespuestaUsuario = 0;
            }
        }else{
            $RespuestaUsuario = 0;
        }
        // END SELECCIONAMOS EL VALOR ACTUAL DEL USUARIO
        // *****************************************************************************
    }

    $_y = 0;
    $select_cat_finished .= "<div id=\"".$idChangeSize."\">";
    do {

        // ********** SI ES OBLIGATORIA *****************
        if($start_cat_valida == 1) {
            if($_y == 0) {
                $JavaIA_check .= "(forma.pregunta_cat".$row_01['id_GuionPregunta']."_".$_y.".checked == false)";
            }else{
                $JavaIA_check .= " && (forma.pregunta_cat".$row_01['id_GuionPregunta']."_".$_y.".checked == false)";
            }
        }
        // ********** END SI ES OBLIGATORIA *************

        if(in_array($rowrrselect_select_cat[$row_01['catalogoId']],$RespuestaUsuarioArreglo)) {
          $selectThis = "checked";
        }else{
          $selectThis = "";
        }

        $select_cat_finished .=  "<input type=\"checkbox\" name=\"pregunta_cat".$row_01['id_GuionPregunta']."_".$_y."\" value=\"".$rowrrselect_select_cat[$row_01['catalogoId']]."\" ".$enabled_cat." ".$selectThis.">".$LetraActivacion.$rowrrselect_select_cat[$row_01['catalogoText']]."<br>\n";

    $_y++;
    }while ($rowrrselect_select_cat = mssql_fetch_array($rselect_select_cat));

    $select_cat_finished .= "</div>";

    // ********** SI ES OBLIGATORIA *****************
    if($start_cat_valida == 1) {
        $JavaIA_check .= " && (error == 0)) {
        alert(\"".$row_01['msgObligatoria']."\");
        error = 1;
        }\n";
    }
    // ********** END SI ES OBLIGATORIA *************
  
}

}else{
    $select_cat_finished = "<select name=\"\" disabled readonly>\n";
    $select_cat_finished .=  "<option value=\"0\">No hay resultados</option>\n";
    $select_cat_finished .= "</select>\n";
}

}


